package com.example.teamtribe.main.club.clubintroduce.media.photo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.FileModel;
import com.example.teamtribe.main.club.clubmembers.MembersModel;

import java.util.ArrayList;

/**
 * Created by lenovo on 2018/6/20.
 */

public class PhotoPresenter implements PhotoContract.Presenter {
    private PhotoContract.View view;
    private PhotoModel model;

    public PhotoPresenter(PhotoContract.View view) {
        this.view = view;
        model =new PhotoModel();
    }

    @Override
    public void getPhoto(String photoName) {
        FileModel.getInstance().download(photoName, 2, new FileModel.FileBack() {
            @Override
            public void loadSuccess(String fileName) {
                view.addPhoto(fileName);
                Log.d("zsm", "photo-download-Success: ");
            }

            @Override
            public void success(@Nullable Bundle bundle) {

            }

            @Override
            public void failure(String meg) {
                view.finishRefresh();
                view.showToast(meg);
            }
        });
    }

    @Override
    public void getPhotos(int teamId, int albumId, int offset, int num) {
        Log.d("zsm", "getPhotos: ");
        model.getPhotoNames(teamId,albumId, offset,num,new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                ArrayList<String> photoNames = bundle.getStringArrayList("fileNames");
                view.clearPhotos();
                if(photoNames.isEmpty()){
                    view.finishRefresh();
                    return;
                }
                for (int i = 0; i < photoNames.size(); i++) {
                    getPhoto(photoNames.get(i));
                    if (i == photoNames.size() - 1) {
                        view.finishRefresh();
                    }
                }
            }

            @Override
            public void failure(String meg) {
                view.finishRefresh();
                view.showToast(meg);
            }
        });
    }

    @Override
    public void getMore(int teamId, int albumId, int offset, int num) {
        Log.d("zsm", "getMorePhotos: ");
        model.getPhotoNames(teamId,albumId, offset,num,new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                ArrayList<String> photoNames = bundle.getStringArrayList("fileNames");
                if(photoNames.isEmpty()){
                    view.finishRefresh();
                    return;
                }
                for (int i = 0; i < photoNames.size(); i++) {
                    getPhoto(photoNames.get(i));
                    if (i == photoNames.size() - 1) {
                        view.finishRefresh();
                    }
                }
            }

            @Override
            public void failure(String meg) {
                view.finishRefresh();
                view.showToast(meg);
            }
        });
    }

    @Override
    public void hasAuthority(int teamId,final int userId) {
        new MembersModel().getPresident(teamId,new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                for (int i = 0; i < ids.size(); i++) {
                    if (userId == ids.get(i)){
                        view.setVisibility();
                    }
                }
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });
        new MembersModel().getAdmins(teamId,new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                for (int i = 0; i < ids.size(); i++) {
                    if (userId == ids.get(i)){
                        view.setVisibility();
                    }
                }
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });
    }
}